package model

import (
	"context"
	"demo/internal/errorx"
	"strconv"

	// "demo/internal/errorx"
	"fmt"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/core/stringx"
)

var _ SevSevClfModel = (*customSevSevClfModel)(nil)

type (
	// SevSevClfModel is an interface to be customized, add more methods here,
	// and implement the added methods in customSevSevClfModel.
	SevSevClfModel interface {
		sevSevClfModel
		withSession(session sqlx.Session) SevSevClfModel
		BulkInsert(ctx context.Context, session sqlx.Session, scIds []int64, sId int64) error
		BulkDelete(ctx context.Context,session sqlx.Session,scIds []int64,sId int64)error
		DeleteBySId(ctx context.Context,session sqlx.Session,sId int64)error
		DeleteBySCId(ctx context.Context,session sqlx.Session,scId int64)error
		DeleteBuilder() squirrel.DeleteBuilder
	}

	customSevSevClfModel struct {
		*defaultSevSevClfModel
	}
)

// NewSevSevClfModel returns a model for the database table.
func NewSevSevClfModel(conn sqlx.SqlConn) SevSevClfModel {
	return &customSevSevClfModel{
		defaultSevSevClfModel: newSevSevClfModel(conn),
	}
}

func (m *customSevSevClfModel) withSession(session sqlx.Session) SevSevClfModel {
	return NewSevSevClfModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customSevSevClfModel) BulkInsert(ctx context.Context, session sqlx.Session, scIds []int64, sId int64) error {
	
	sql := fmt.Sprintf("insert into public.sev_sev_clf(sev_id,sev_clf_id) values (?,?)")

	blk,err := sqlx.NewBulkInserter(sqlx.NewSqlConnFromSession(session),sql)
	if err != nil {
		panic(err)
	}
	defer blk.Flush()
	for _,val := range scIds{
		blk.Insert(sId,val)
	}
	return nil
}

func (m *customSevSevClfModel)BulkDelete(ctx context.Context,session sqlx.Session,scIds []int64,sId int64)error{
	sql := fmt.Sprintf("delete from sev_sev_clf where sev_id = $1 and sev_clf_id in (")

	values := make([]interface{},0)
	values = append(values, sId)
	for idx,val := range scIds{
		now := strconv.FormatInt(int64(idx + 2), 10)
		sql = sql + "$" + now + ","
		values = append(values, val)
	}
	sql,err := stringx.Substr(sql,0,len(sql) - 1)
	if err != nil {
		return errorx.NewBizError(500,"SQL语句转化失败")
	}
	sql = sql + ")"

	if session != nil {
		_,err = session.ExecCtx(ctx,sql,values...)
	}else{
		_,err = m.conn.ExecCtx(ctx,sql,values...)
	}
	if err != nil {
		return errorx.NewBizError(500,"SQL执行错误")
	}
	return nil
}

func (m *customSevSevClfModel)DeleteBySId(ctx context.Context,session sqlx.Session,sId int64)error{
	builder := m.DeleteBuilder().Where(squirrel.Eq{"sev_id":sId})
	query,values,err := builder.ToSql()
	if err != nil {
		return err
	}
	_,err = m.conn.ExecCtx(ctx,query,values...)
	return err
}
func (m *customSevSevClfModel)DeleteBySCId(ctx context.Context,session sqlx.Session,scId int64)error{
	builder := m.DeleteBuilder().Where(squirrel.Eq{"sev_clf_id":scId})
	query,values,err := builder.ToSql()
	if err != nil {
		return err
	}
	_,err = m.conn.ExecCtx(ctx,query,values...)
	return err
}

func (m *customSevSevClfModel)DeleteBuilder() squirrel.DeleteBuilder{
	return squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).Delete(m.table)
}
